package model;

import model.Blog;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BlogDao {
    //往博客表中插入一个博客
    public  void insert(Blog blog) {
        PreparedStatement statement=null;
        Connection connection=null;
        try {
            //1.和数据库建立连接
           connection = DBUtil.getConnection();
            //2.构造SQL语句
            String sql = "insert into blog values (null,?,?,?,now())";//now()就是当前时间
            statement= connection.prepareStatement(sql);
            statement.setString(1,blog.getTitle());//博客标题
            statement.setString(2,blog.getContent());//博客文章内容
            statement.setInt(3,blog.getAuthorId());//博客作者Id
            //执行SQL语句
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //关闭资源
            DBUtil.close(statement,connection,null);
        }
    }
    //查看博客表中所有博客信息--博客列表页需要
    public List<Blog> selectAll(){
        PreparedStatement statement=null;
        Connection connection=null;
        //结果集
        ResultSet resultSet=null;
        List<Blog>blogs=new ArrayList<Blog>();
        try {
            //连接数据库
            connection= DBUtil.getConnection();
            //构造SQL语句
            String sql="select * from blog order by postTime desc";
            statement=connection.prepareStatement(sql);
            //执行SQL语句
            resultSet=statement.executeQuery();
            //遍历结果集
            while(resultSet.next()){
                Blog blog=new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                String content=resultSet.getString("content");
                //文章内容过长，进行截断
                if(content.length()>50){
                    content=content.substring(0,50)+".....";
                }
                blog.setContent(content);
                blog.setAuthorId(resultSet.getInt("authorId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blogs.add(blog);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //关闭资源
            DBUtil.close(statement,connection,resultSet);
        }
        return blogs;
    }
    //根据博客id查找指定博客---博客详情页
    public Blog selectOne(int blogId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            resultSet = statement.executeQuery();
            // 此处我们是使用 主键 来作为查询条件的.
            if (resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setAuthorId(resultSet.getShort("authorId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                return blog;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close( statement,connection, resultSet);
        }
        return null;
    }
    //根据博客id删除博客
    public  void delete(int blogId){
        PreparedStatement statement=null;
        Connection connection=null;
        try {
            //连接数据库
            connection= DBUtil.getConnection();
            //构造SQL语句
            String sql="delete  from blog where blogId=?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            //执行SQL语句
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //关闭资源
            DBUtil.close(statement,connection,null);
        }
    }
}
